//
//  VieoListViewController.m
//  VideoPlayer
//
//  Created by mac on 2019/4/23.
//  Copyright © 2019 YangYang. All rights reserved.
//

#import "VieoListViewController.h"
#import "NetVideoViewController.h"

@interface VieoListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation VieoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableView];
    [self initDataSource];
}

-(void)initDataSource
{
    NSArray *dataArr = [NSArray arrayWithContentsOfFile:_videoPath];
    NSLog(@"dataArr = %@",dataArr);
    NSArray *modelArr = [VideoModel mj_objectArrayWithKeyValuesArray:dataArr];
    [self.dataSource addObjectsFromArray:modelArr];
    [self.tableView reloadData];
}

-(void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    //    NSDictionary *videoInfo = _dataSource[indexPath.row];
    VideoListModel *model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.videoName;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    VideoModel *model = self.dataSource[indexPath.row];
    
    NetVideoViewController *netVideoVC = [[NetVideoViewController alloc] init];
    netVideoVC.netVideoName = model.videoName;
    netVideoVC.netVideoURL = model.videoURL;
    netVideoVC.videoArr = self.dataSource;
    netVideoVC.videoIndex = indexPath.row;
    netVideoVC.navigationItem.title = model.videoName;
    [self.navigationController pushViewController:netVideoVC animated:YES];
    
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
